import java.io.*;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2024-10-25
 * Time: 16:45
 */
public class MyArrayList implements IList{
    //第一关：顺序线性表创建（课程信息初始化）

    //（1）定义一个类储存每个课程的基本信息
    static class node{
        String name;
        double credit;
        int hour;
        int practice;
        String category;
        int semester;

        public node(String name, double credit, int hour, int practice, String category, int semester) {
            this.name = name;
            this.credit = credit;
            this.hour = hour;
            this.practice = practice;
            this.category = category;
            this.semester = semester;
        }

        @Override
        public String toString() {
            return name+" "+credit+" "+hour+" "
                    +practice+" "+category+" "+semester+"\n";
        }
    }
    //（2）定义顺序线性表
    int max=20;
    node elem[]=new node[max];
    int usedsize=0;



    @Override
    //顺序表实验
    //从couse.txt读取文件已有课程信息
    //任务二（4）完成insertFromFile
    public void insertFromFile() {
        if(isfull()){
            elem=new node[max*2].clone();
        }
        String filePath = "C:/Users/LIUYANJIAO/Desktop/couse.txt";
        try (FileReader fileReader = new FileReader(filePath);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                // 对每一行的内容进行处理
                String[] arr=new String[6];
                arr=line.split(" ");
                elem[usedsize]=new node(arr[0],Double.parseDouble(arr[1]),Integer.parseInt(arr[2]),
                        Integer.parseInt(arr[3]),arr[4],Integer.parseInt(arr[5]));
                usedsize++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //（4）完成printList_seq函数，能按顺序表中顺序输出课程信息
    @Override
    public void printList_seq() {
        for (int i = 0; i < usedsize; i++) {
            System.out.print(elem[i].toString());
        }
        System.out.println();
    }

    //第二关：顺序线性表插入（课程信息插入）
    //（1）实现判断线性表是否为满的判断函数
    public boolean isfull(){
        if(usedsize>max){
            return true;
        }else {
            return false;
        }
    }
    //(2)完成p之前和p之后的插入函数
    @Override
    public void insertPre_seq(int p,node n) {
        if(p>usedsize||p<0) return;
        for (int i = usedsize; i >p; i--) {
           elem[i]=elem[i-1];
        }
        elem[p]=n;
        usedsize++;
    }

    @Override
    public void insertPost_seq(int p,node n) {
        if(p>usedsize||p<0) return;
        for (int i = usedsize; i >p+1; i--) {
            elem[i]=elem[i-1];
        }
        elem[p+1]=n;
        usedsize++;
    }

    //（3）完成findPos函数
    @Override
    public int findPos(node n) {
        for (int i = 0; i < usedsize; i++) {
            if(elem[i].name.equals(n.name)) return i;
        }
        return -1;
    }


    //（5）完成writeToFile函数

    @Override
    public void writeToFile() {


            String filePath = "C:/Users/LIUYANJIAO/Desktop/" + "couse1.txt";
            FileWriter fw = null;
            try
            {
                File file = new File(filePath);
                if (!file.exists())
                {
                    file.createNewFile();
                }
                fw = new FileWriter(filePath);
                BufferedWriter bw=new BufferedWriter(fw);
                for (int i = 0; i < usedsize; i++) {
                    bw.write(elem[i]+"\n");
                }

                bw.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {

                try
                {
                    fw.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }


            }
    }

    //第三关：销毁顺序线性表
    @Override
    public void clear_seq() {
        for (int i = 0; i < usedsize; i++) {
            elem[i]=null;
        }
        usedsize=0;
    }

    //第四关：顺序线性表之顺序查找
    @Override
    public int findName(String name) {
        for (int i = 0; i < usedsize; i++) {
            if(elem[i].name.equals(name)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public node findPos(int pos) {
        if(pos<0||pos>=usedsize) return null;
        return elem[pos];
    }

    //第五关：顺序线性表删除
    @Override
    public void delpos(int pos) {
        if(pos<0||pos>=usedsize) return;
        if(pos==usedsize-1){
            usedsize--;
            return;
        }
        for (int i = pos; i < usedsize - 1; i++) {
            elem[i]=elem[i+1];
        }
        usedsize--;
    }

    @Override
    public void delName(String name) {
        int pos=findName(name);
        if(pos==-1) return;
        delpos(pos);
    }

    //第六关：顺序线性表
    //（1）能统计每个学期所开课程的学分、学时


    @Override
    public void sumAll() {
        double[] suncre=new double[8];
        double[] sumhour=new double[8];
        for (int i = 0; i < usedsize; i++) {
            switch(elem[i].semester){
                case 1:
                    suncre[0]+=elem[i].credit;
                    sumhour[0]+=elem[i].hour+elem[i].practice;
                    break;
                case 2:
                    suncre[1]+=elem[i].credit;
                    sumhour[1]+=elem[i].hour+elem[i].practice;
                    break;
                case 3:
                    suncre[2]+=elem[i].credit;
                    sumhour[2]+=elem[i].hour+elem[i].practice;
                    break;
                case 4:
                    suncre[3]+=elem[i].credit;
                    sumhour[3]+=elem[i].hour+elem[i].practice;
                    break;
                case 5:
                    suncre[4]+=elem[i].credit;
                    sumhour[4]+=elem[i].hour+elem[i].practice;
                    break;
                case 6:
                    suncre[5]+=elem[i].credit;
                    sumhour[5]+=elem[i].hour+elem[i].practice;
                    break;
                case 7:
                    suncre[6]+=elem[i].credit;
                    sumhour[6]+=elem[i].hour+elem[i].practice;
                    break;
                case 8:
                    suncre[7]+=elem[i].credit;
                    sumhour[7]+=elem[i].hour+elem[i].practice;
                    break;
            }

        }
        for (int j = 0; j < 8; j++) {
            System.out.println("第"+(j+1)+"学期:"+" 学分："+suncre[j]+"  学时："+sumhour[j]);
        }
        System.out.println();
    }

    //（2）能统计每个类别（例如必修、选修等）课程的学时（包括讲授学时和实践学时）和学分，
    // 并统计学分占比
    @Override
    public void cutHour() {
        double[] suncre=new double[2];
        double[] sumhour=new double[2];
        for (int i = 0; i < usedsize; i++) {
            switch (elem[i].category){
                case "BT":
                    suncre[0]+=elem[i].credit;
                    sumhour[0]+=elem[i].hour+elem[i].practice;
                    break;
                case "BJ":
                    suncre[1]+=elem[i].credit;
                    sumhour[1]+=elem[i].hour+elem[i].practice;
                    break;
            }
        }
        double[] percentage=new double[2];
        percentage[0]=suncre[0]/(suncre[0]+suncre[1]);
        percentage[1]=suncre[1]/(suncre[0]+suncre[1]);
        for (int j = 0; j < 2; j++) {
            System.out.println(elem[j].category+"类型： "+" 学分："+suncre[j]+"  学时："+sumhour[j]+"  学分占比："+percentage[j]);
        }
        System.out.println();
    }


}
